//
//  BRCouponsBaseCell.m
//  Kexu
//
//  Created by IOS on 2023/7/25.
//

#import "BRCouponsBaseCell.h"

@interface BRCouponsBaseCell ()
{
    @public
    BRCouponModel *_dto;
}
@end
@implementation BRCouponsBaseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUI];
    }
    return self;
}

- (void)setDto:(BRCouponModel *)dto {
    _dto = dto;
}

#pragma mark - UI --
- (void)setUI {
    self.contentView.backgroundColor = UIColor.clearColor;
    self.backgroundColor = UIColor.clearColor;
    
    [self.contentView addSubview:self.contView];
    [self.contView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(14);
        make.left.offset(14);
        make.right.offset(-14);
        make.bottom.offset(0);
    }];
    
    self.contView.layer.cornerRadius = 10;
    self.contView.layer.masksToBounds = YES;
    
    
    CGRect frame = CGRectMake(0, 0, kScreenWidth - 28, 100);
    UIView *topView = [[UIView alloc] initWithFrame:frame];
    topView.backgroundColor = UIColor.whiteColor;
    [self.contView addArrangedSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_greaterThanOrEqualTo(100);
    }];
    self.topView = topView;
    
    UIImage *bubble = UIImageMake(@"coupon_line");
    UIImageView *lineImg = [[UIImageView alloc] init];
    lineImg.image = [bubble resizableImageWithCapInsets:UIEdgeInsetsMake(10, 0, 10, 0) resizingMode:UIImageResizingModeStretch];
    [self.leftView addSubview:lineImg];
    [lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(5);
        make.top.offset(0);
        make.width.mas_equalTo(8);
        make.bottom.offset(0);
    }];
    self.lineImg = lineImg;
    
    [topView addSubview:self.leftView];
    [topView addSubview:self.rightView];
    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.offset(0);
        make.right.offset(-108);
    }];
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.offset(0);
        make.left.equalTo(self.leftView.mas_right).offset(0);
    }];
    
    self.bottomView.hidden = YES;
    [self.contView addArrangedSubview:self.bottomView];
    
    UIView *line = [BRToolsView getLineView];
    [self.bottomView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.offset(0);
        make.height.mas_equalTo(0.5);
    }];
}

- (UIStackView *)contView {
    if (_contView == nil) {
        _contView = [[UIStackView alloc] init];
        _contView.axis = UILayoutConstraintAxisVertical;
    }
    return _contView;
}
- (UIView *)leftView {
    if (_leftView == nil) {
        _leftView = [[UIView alloc] init];
    }
    return _leftView;
}
- (UIView *)rightView {
    if (_rightView == nil) {
        _rightView = [[UIView alloc] init];
    }
    return _rightView;
}
- (UIView *)bottomView {
    if (_bottomView == nil) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = UIColor.whiteColor;
    }
    return _bottomView;
}

//- (UIImageView *)bgImg {
//
//    if (_bgImg == nil) {
//        _bgImg = [[UIImageView alloc] init];
//    }
//    return _bgImg;
//}

@end


@implementation BRListCouponsCell


- (void)setType:(NSInteger)type {
    _type = type;
    
    self.leftView.alpha = type != 1 ? 0.2 : 1;
    self.ruleView.alpha = self.leftView.alpha;
    self.openBtn.enabled = type <= 1;
}

- (void)toOpen {
    
    
    
    if (self.openBlock) self.openBlock(YES);
    self.open = !self.open;
    if (self.openBlock) self.openBlock(NO);
}


- (void)setOpen:(BOOL)open {
    _open = open;
    
    [self br_setLeftH];
    
    self.bottomView.hidden = !open;
    WS(weakSelf);
    if (open) {
        
        [UIView animateWithDuration:0.25 animations:^{
            
            weakSelf.tcView.imgBg.transform = CGAffineTransformMakeRotation(M_PI);
            self.bottomView.alpha = 1;
        } completion:^(BOOL finished) {
            
        }];
        
    }else {
//        self.bottomView.hidden = !open;
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.tcView.imgBg.transform = CGAffineTransformMakeRotation(0);
            self.bottomView.alpha = 0;
        } completion:^(BOOL finished) {
            
        }];
        
    }
    
   
}

- (void)br_setLeftH {
    
    if (self.leftH != 0) return;;
    self.leftH = self.leftView.mj_h;
    if (self.leftH < 100) {
        self.leftH = 100;
    }
    [self.leftView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(0);
        make.right.offset(-108);
        make.height.mas_equalTo(self.leftH);
    }];
    [self.topView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.height.mas_equalTo(self.leftH);
    }];
    [self.lineImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(5);
        make.top.offset(0);
        make.width.mas_equalTo(8);
        make.height.mas_equalTo(self.leftH);
    }];
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.offset(0);
        make.left.equalTo(self.leftView.mas_right).offset(0);
        make.height.mas_equalTo(self.leftH);
    }];
}


- (void)setDto:(BRCouponModel *)dto {
    [super setDto:dto];
    
    self.timeL.text = [NSString stringWithFormat:@"%ld %@",dto.point,Localized(@"points_pts")];
    
    
//    NSString *currency = USERManager.priceSymbol;
    
    // 抵扣类型：1-金额 2-时间 3-折扣
    NSInteger couponType = dto.deductionType;
    NSString *title1 = [BRStringTools br_couponsTypeStr:couponType];
    NSString *text1 = [BRStringTools br_couponsTypeContentV2:dto.deduction];
    self.typeL.text = title1;
    
    self.amountView.amountL.text = text1;
    
    self.timeL.text = [NSString stringWithFormat:@"%@: %@",Localized(@"order_expiryDate"),[BRTools getTimeFromTimestamp:dto.endTime.integerValue formatter:kTimeDMYFormatter]];
    
    // 状态：1-未使用 2-已使用 3-已过期 state
    self.amountView.type = dto.state;
//    CGFloat phla =  dto.state != 1 ? 0.2 : 1;
    self.type = dto.state;
    // 底部描述
    NSString *shopNames = dto.merchantNameList.length != 0 ? dto.merchantNameList : Localized(@"coupon_allStationUse");
    
    NSString *lanKey = [[[BRLanguageManager share] requestStr] lowercaseString];
    NSString *text = [dto.descriptionStr valueForKey:lanKey];
    self.ruleView.tcView.textTC.text = text;
    self.ruleView.locationView.textTC.text = shopNames;
}

#pragma mark - ----------UI
- (void)setUI {
    [super setUI];
    
    //------- left
    self.typeL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontS:17]];
    self.timeL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:13]];
    
    self.tcView = [[BRImgLabelView alloc] init];
    
    [self.tcView.img mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(16);
        
    }];
    self.tcView.imgBg.backgroundColor = UIColor.qd_tintColor3;
    self.tcView.imgBg.layer.masksToBounds = YES;
    self.tcView.imgBg.layer.cornerRadius = 7;
    self.tcView.titleL.font = [UIFont fontM:10];
    self.tcView.img.image = UIImageMake(@"coupon_down");
    self.tcView.titleL.text = Localized(@"coupon_viewCouponTips");
    
    UIStackView *stack = [[UIStackView alloc] init];
    stack.spacing = 6;
    stack.axis = UILayoutConstraintAxisVertical;
    [stack addArrangedSubview:self.timeL];
    [stack addArrangedSubview:self.tcView];
    self.timeL.numberOfLines = 0;
    self.typeL.numberOfLines = 0;
    [self.leftView addSubview:self.typeL];
    [self.typeL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.top.offset(12);
        make.right.offset(-8);
    }];
    
    [self.leftView addSubview:stack];
    [stack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(14);
        make.right.offset(-8);
//        make.top.offset(48);
        make.top.equalTo(self.typeL.mas_bottom).offset(14);
        make.bottom.offset(-15);
    }];
    UIControl *btn = [[UIControl alloc] init];
    [self.tcView addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    [btn addTarget:self action:@selector(toOpen) forControlEvents:UIControlEventTouchUpInside];
    _openBtn = btn;
    //---------- right
    [self br_addRightView];
    
    //---------- bottomView
    self.bottomView.alpha = 0;
    [self.bottomView addSubview:self.ruleView];
    [self.ruleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(14);
        make.right.bottom.offset(-14);
//        make.height.mas_equalTo(50);
    }];
    
}

- (void)br_addRightView; {
    [self.rightView addSubview:self.amountView];
    [self.amountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.bottom.offset(0);
    }];
}

- (BRCouponsRuleView *)ruleView {
    
    if (_ruleView == nil) {
        
        _ruleView = [[BRCouponsRuleView alloc] init];
    }
    return _ruleView;
}

- (BRCouponAmountView *)amountView {
    
    if (_amountView == nil) {
        
        _amountView = [[BRCouponAmountView alloc] init];
    }
    return _amountView;
}

@end

@implementation BRCouponCenterCell


- (void)setListDto:(BRCouponListModel *)listDto  {
//    [super setDto:dto];
    _listDto = listDto;
    BRCouponListModel *dto = listDto;
    self.timeL.text = [NSString stringWithFormat:@"%ld %@",dto.exchangePoint.integerValue,Localized(@"points_pts")];
    
    self.progressView.progressView.progress = dto.redeemedRate.floatValue;
    self.progressView.titleL.text = [NSString stringWithFormat:@"%@: %.0f%%",Localized(@"coupon_redeemed"),dto.redeemedRate.floatValue * 100];
    
//    NSString *currency = USERManager.priceSymbol;
    
    // 抵扣类型：1-金额 2-时间 3-折扣
    NSInteger couponType = dto.deductionType;
    NSString *title1 = [BRStringTools br_couponsTypeStr:couponType];
    NSString *text1 = [BRStringTools br_couponsTypeContentV2:dto.deduction];
    
    self.typeL.text = [NSString stringWithFormat:@"%@ %@",title1,text1];
    
    // 积分比对
    self.emptyPoint = dto.exchangePoint.integerValue > [BRUserManager share].point;
    
    // 底部描述
    NSString *shopNames = dto.merchantNameList.length != 0 ? dto.merchantNameList : Localized(@"coupon_allStationUse");
    
    NSString *lanKey = [[[BRLanguageManager share] requestStr] lowercaseString];
    NSString *text = [dto.descriptionStr valueForKey:lanKey];
    self.ruleView.tcView.textTC.text = text;
    self.ruleView.locationView.textTC.text = shopNames;
}

- (void)setEmptyPoint:(BOOL)emptyPoint {
    _emptyPoint = emptyPoint;
    
    self.progressView.redeemBtn.hidden = emptyPoint;
    self.progressView.textL.hidden = !emptyPoint;
}

- (void)br_addRightView {
    [self.rightView addSubview:self.progressView];
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.offset(0);
//        make.top.bottom.offset(0);
        make.left.right.offset(0);
        make.height.mas_equalTo(100);
        make.centerY.offset(0);
    }];
}

- (BRCouponNumView *)progressView {
    
    if (_progressView == nil) {
        _progressView = [[BRCouponNumView alloc] init];
    }
    
    return _progressView;
}

@end

#pragma mark - BRCouponAmountView --
@implementation BRCouponAmountView


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}
// 状态：1-未使用 2-已使用 3-已过期 state
- (void)setType:(NSInteger)type {
    _type = type;
    
    self.amountL.alpha = type != 1 ? 0.2 : 1;
    
    self.slopingView.hidden = type == 1;
    
    NSString *typeStr = type == 3 ? @"coupon_expired" : @"coupon_used";
    self.slopingView.title = Localized(typeStr);
}


#pragma mark - UI --
- (void)setUI {
    
    self.amountL = [BRToolsView br_label:@"0.00" textColor:kColorMain font:[UIFont fontS:23]];
    self.amountL.numberOfLines = 2;
    self.amountL.textAlignment = NSTextAlignmentCenter;
    self.amountL.adjustsFontSizeToFitWidth = YES;
    [self addSubview:self.amountL];
    [self addSubview:self.slopingView];
    [self.amountL mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerY.offset(0);
        make.left.offset(2);
        make.right.offset(-2);
    }];
    
    [self.slopingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(84);
        make.top.right.offset(0);
        
    }];
    
    
}

- (BRSlopingView *)slopingView {
    
    if (_slopingView == nil) {
        _slopingView = [[BRSlopingView alloc] init];
    }
    return _slopingView;
}


@end

@implementation BRCouponNumView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)redeemDo {
    if (self.redeemBlock) self.redeemBlock();
}


#pragma mark - UI --
- (void)setUI {
    
    UIProgressView *progress = [[UIProgressView alloc] init];
    progress.layer.cornerRadius = 3;
    progress.layer.masksToBounds = YES;
    progress.backgroundColor = UIColorMakeWithHex(@"#D8D8D8");
    progress.progress = 0.8;
    progress.progressTintColor = kColorMain;
    [self addSubview:progress];
    
    [progress mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-14);
        make.left.offset(14);
        make.top.offset(18);
        make.height.mas_equalTo(6);
    }];
    _progressView = progress;
    
    self.titleL = [BRToolsView br_label:@"--" textColor:kColorTitle66B font:[UIFont fontM:10] textAlignment:NSTextAlignmentCenter];
    self.titleL.numberOfLines = 2;
    [self addSubview:self.titleL];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.offset(0);
        make.top.equalTo(progress.mas_bottom).offset(6);
    }];
    
    self.redeemBtn = [[QMUIButton alloc] init];
    [self.redeemBtn setTitle:Localized(@"coupon_redeemNow") forState:UIControlStateNormal];
    [self.redeemBtn setBackgroundColor:kColorMain];
    [self.redeemBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    self.redeemBtn.titleLabel.font = [UIFont fontM:12];
    self.redeemBtn.layer.cornerRadius = 7;
    self.redeemBtn.layer.masksToBounds = YES;
    [self addSubview:self.redeemBtn];
    [self.redeemBtn addTarget:self action:@selector(redeemDo) forControlEvents:UIControlEventTouchUpInside];
    [self.redeemBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.height.mas_equalTo(30);
        make.top.equalTo(self.titleL.mas_bottom).offset(6);
    }];
    
    self.textL = [BRToolsView br_label:Localized(@"coupon_insufficientPoints") textColor:kColorTitle66B font:[UIFont fontM:12] textAlignment:NSTextAlignmentCenter];
    self.textL.numberOfLines = 3;
    self.textL.hidden = YES;
    [self addSubview:self.textL];
    [self.textL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.equalTo(self.titleL.mas_bottom).offset(6);
    }];
    
}

@end
